package com.cy.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.mapper.MappingMapper;
import com.cy.mapper.Nmapper;
import com.cy.pojo.Mapping;
import com.cy.pojo.Nuser;

import com.cy.pojo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author cy
 */
@Service
@Slf4j
public class NserviceImpl  implements Nservice {

    private static AtomicInteger count = new AtomicInteger(0);

    @Autowired
    private Nmapper nmapper;

    @Autowired
    private MappingMapper baseMapper;

    @Override
    public Nuser findById(Integer id) {
        return nmapper.selectById(id);
    }

    @Override
    public List<Nuser> findObjects() {
        List<Nuser> userInfoList = nmapper.selectList(null);
        return userInfoList;
    }

    @Transactional
    @Override
    public void saveUser(UserVO userVO) {
        nmapper.saveObject(userVO);
       // nmapper.insert(userVO);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public Nuser findByName(String name) {
            if (StringUtils.isNotEmpty(name)) {
                Mapping mapping = baseMapper.getMappingByName(name);
                Nuser nuser = null;
                if (mapping != null && StrUtil.isNotEmpty(mapping.getOldName())) {
                    String oldName = mapping.getOldName();
                    Nuser user = getUser(oldName);
                    if (user == null) {
                        log.info("{} 老名字没查到查询新名字  {} ",Thread.currentThread().getName());
                        return getUser(name);
                    } else {
                        log.info("{} 查询老名字  {} ",Thread.currentThread().getName());
                        return user;
                    }
                } else {
                    log.info("{} 根据新名字查询  {} ",Thread.currentThread().getName());
                    return getUser(name);
                }
            }
            return null;
    }

    @Transactional
    @Override
    public int updateUser(Nuser nuser) {
         return nmapper.updateById(nuser);
    }

    @Transactional
    @Override
    public int deleteUser(Integer id) {
       return nmapper.deleteById(id);
    }

    @Transactional
    @Override
    public int updateBatch(List<Integer> ids) {
      int rows= nmapper.updateBatch(ids);
      return rows;
    }

    @Transactional
    @Override
    public int deleteBatch(@Valid List<Integer> ids) {
      int rows=nmapper.deleteBatch(ids);
      return rows;
    }

    @Transactional
    @Override
    public int saveBatchNuser(List<Nuser> nuserList) {
       int rows=nmapper.saveBatchNuser(nuserList);
       return rows;
    }

    @Override
    public List<Nuser> findBatchNuser(@Valid List<Integer> ids) {
        List<Nuser>nuserList=nmapper.findBatchUser(ids);
        return nuserList;
    }

    @Override
    public List<Nuser> findByNameColl(String name) {
        return nmapper.findByNameCollection(name);
    }

    public Nuser getUser(String name){
        List<Nuser> nuserList = nmapper.findByName(name);
        if(nuserList != null && nuserList.size() > 0){
            Nuser nuser = nuserList.get(0);
            nuser.setStatus(1);
            int n = nmapper.updateById(nuser);
            if(n != 0){
                return nuser;
            }else{
                log.info("执行了第"+count.incrementAndGet()+"次递归");
                getUser(name);
            }
        }
        return null;
    }

    @Override
    public IPage<Nuser> selectUserPage(Map map) {
        String newName = (String) map.get("newName");
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");

        Page<Nuser> page=new Page<>(pageNum,pageSize);
       // QueryWrapper<Nuser>queryWrapper=new QueryWrapper<>();
        return nmapper.selectPageVo(page, newName);
    }

    @Override
    public IPage<Nuser> selectPageObj(int pageCurrent, int pageSize) {
        Page<Nuser>page=new Page<>(pageCurrent,pageSize);
        QueryWrapper<Nuser>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("new_name", "王麻子");
        return nmapper.selectPage(page, queryWrapper);
    }
}
